#include<brpc/server.h>
#include<butil/logging.h>
#include"main.pb.h"
class EchoServiceImpl:public example::EchoService
{
public:
    void Echo(google::protobuf::RpcController* controller,
                       const ::example::EchoRequest* request,
                       ::example::EchoResponse* response,
                       ::google::protobuf::Closure* done)
    {
        brpc::ClosureGuard rpc_guard(done);//和RAII思想类似，在局部域结束后自动调用done->Run();
        std::cout<<"收到消息:"<<request->message()<<std::endl;
        std::string res = "我收到了你的:"+request->message()+"消息,并可以给你回复";
        response->set_message(res);
    }//EchoServiceImpl服务的一个接口，作用是对收到的信息进行回复
};



int main()
{
    //关闭brpc的默认日志输出
    logging::LoggingSettings logset;
    logset.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(logset);
    //1.构造服务器对象
    brpc::Server server;
    //2.向服务器新增服务
    EchoServiceImpl echo_service;
    int ret = server.AddService(&echo_service,brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);
    if(ret == -1)
    {
        std::cout<<"添加RPC服务失败\n";
        return -1;
    }
    //3.启动服务器
        //测试一下对服务器的配置
        brpc::ServerOptions options;
        options.idle_timeout_sec = -1;//配置连接空闲超时时间，超时后连接被关闭
        options.num_threads = 1;//io线程数量，默认是CPU核数
    ret = server.Start(8080,&options);
    if(ret==-1)
    {
        std::cout<<"服务器启动失败!\n";
        return -1;
    }
    server.RunUntilAskedToQuit();
    return 0;
};